package model

import (
	"gorm.io/gorm"
)

type User struct {
	gorm.Model
	Account   string `gorm:"type:varchar(30);"`
	Password  string `gorm:"type:varchar(50)"`
	Nickname  string `gorm:"type:varchar(50)"`
	Avatar    string `gorm:"type:varchar(225)"`
	Signature string `gorm:"type:varchar(50)"`
}

type Friend struct {
	gorm.Model
	UserId   int    `gorm:"int"`
	FriendId int    `gorm:"int"`
	Text     string `gorm:"type:varchar(225)"`
	Status   int    `gorm:"int"` //1.同意,2.解决,3.待处理
}
type BlackD struct {
	gorm.Model
	UserId   int `gorm:"int"`
	FriendId int `gorm:"int"`
}

type Moments struct {
	gorm.Model
	UserId int    `gorm:"int"`
	Text   string `gorm:"type:varchar(225)"`
	Like   int    `gorm:"int"`
}
type Likes struct {
	gorm.Model
	UserId    int `gorm:"int"`
	MomentsId int `gorm:"int"`
	Like      int `gorm:"int"`
}
type Liao struct {
	gorm.Model
	UserId   int    `gorm:"int"`
	FriendId int    `gorm:"int"`
	Text     string `gorm:"varchar(50)"`
}

func (l *Liao) LiaoAdd(db *gorm.DB) error {
	err := db.Create(&l).Error
	return err
}

func (l *Liao) LiaoList(db *gorm.DB) (list []*Liao, err error) {
	err = db.Model(&l).Order("created_at desc").Find(&list).Error
	return list, err
}

func (b *BlackD) ISBlackD(db *gorm.DB, uid int, fid int) (err error) {
	err = db.Model(&b).Where("user_id = ? and friend_id = ?", uid, fid).First(&b).Error
	return err
}

func (f *Friend) ISFriend(db *gorm.DB, uid int, fid int) (err error) {
	err = db.Model(&f).Where("user_id = ? and friend_id = ?", uid, fid).First(&f).Error
	return err
}

func (l *Likes) LikesAdd(db *gorm.DB) error {
	err := db.Create(&l).Error
	return err
}

func (m *Moments) MomentsList(db *gorm.DB) (list []*Moments, err error) {
	err = db.Model(&m).Order("created_at desc").Find(&list).Error
	return list, err
}

func (m *Moments) MomentsAdd(db *gorm.DB) error {
	err := db.Create(&m).Error
	return err
}

func (b *BlackD) BlackDAdd(db *gorm.DB) error {
	err := db.Create(&b).Error
	return err
}

func (u *User) BlackDList(db *gorm.DB, uid int) (list []*User, err error) {
	err = db.Model(&u).Where("id = ?", uid).Find(&list).Error
	return list, err
}

func (f *Friend) FriendAdd(db *gorm.DB) error {
	err := db.Create(&f).Error
	return err
}
func (f *Friend) FriendUpdate(db *gorm.DB, id string, status string) error {
	err := db.Model(&f).Where("id = ?", id).Updates(status).Error
	return err
}
func (f *Friend) FriendList(db *gorm.DB) (list []*Friend, err error) {
	err = db.Model(&f).Find(&list).Error
	return list, err
}

func (u *User) UserAdd(db *gorm.DB) error {
	err := db.Create(&u).Error
	return err
}

func (u *User) FriendSList(db *gorm.DB, Account string, Nickname string) (list []*User, err error) {
	db.Model(&u).Where("account like ? and nickname like ?", "%"+Account+"%", "%"+Nickname+"%").Find(&list)
	return list, err
}
